// pages/addDevice/addDevice.js
const cloud = require('../../../../utils/cloudFunction')
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        clock: '',
        roomList: [],
        devType: '',
        selectChange: "smallRoom",
        id_text: '',
        tmp: 0,
        text: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // console.log(options)
        if (app.globalData.roomIndex >= 0) {
            this.setData({
                aDImg: options.aDImg,
                devType: options.type,
                roomList: app.globalData.homeList,
                tmp: app.globalData.roomIndex
            })
        } else {
            this.setData({
                aDImg: options.aDImg,
                devType: options.type,
                roomList: app.globalData.homeList
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if (this.data.roomList.length > 0) {
            let id = this.data.roomList[this.data.tmp]
            // console.log(id);
            this.setData({
                id_text: id.roomId
            })
        } else {
            wx.showToast({
                icon: 'loading',
                title: '获取房间列表中',
                duration: 2000
            })
        }

        var that = this
        // getHomeList
        this.data.clock = setInterval(() => {
            // console.log("addPage clocking ... ")
            if (app.globalData.refreshStatus) {
                that.setData({
                    roomList: app.globalData.roomList
                })
                app.globalData.refreshStatus = false
                console.log("addPage update -------------------------")
            }
        }, 500);

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        clearInterval(this.data.clock)
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    onInput: function (e) {
        this.setData({
            text: e.detail.value
        })
    },

    async addDeviceBtn() {

        if (!this.data.text) {
            wx.showToast({
                icon: 'error',
                title: '名称不能为空',
                duration: 1500
            })
            return
        }

        await cloud.saveOrUpdateDevice(this.data.id_text, this.data.text, this.data.devType)
        cloud.changeStatus()
        wx.showToast({
            title: '添加设备成功',
            duration: 2000
        })

        setTimeout(() => {
            // back to index page
            wx.navigateBack({
                delta: 0,
            })
        }, 1000);
    },

    selectRoom: function (e) {
        // set new select roomId 
        this.data.id_text = e.target.dataset.roomid
        this.setData({
            tmp: e.target.dataset.id
        })
    },

    /**
     * 弹出按钮点击事件
     */
    popUp: function (e) {
        // console.log(e.currentTarget.dataset.id)
        this.setData({
            showModal: true
        })
    },
    /**
     * 隐藏
     */
    popDown: function () {
        this.setData({
            showModal: false
        });
    },

    // 添加房间
    async onInputConfirm(e) {
        if (e.detail) {
            this.popDown()
            await cloud.saveOrUpdateRoomInfo(e.detail)
            cloud.changeStatus()
            wx.showToast({
                title: '添加房间成功',
                duration: 2000
            })
        }
    },
})